#!/bin/sh
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

getfield() {
  shift $1
  echo $1
}

# Converts a floating point number of seconds to a properly rounded
# number of integer milliseconds.
seconds_to_ms() {
  echo $1 | awk '{ print int($1 * 1000.0 + 0.5)}'
}

EVENT=$1
shift
if [ $# -eq 0 ]; then
  set -- time
fi

while [ $# -gt 0 ]; do
  case "$1" in
    time)
      TAG=uptime
      FIELD=1
    ;;
    # Emit time in milliseconds instead of fractional seconds.
    time-ms)
      TAG=uptime
      FIELD=1
      EMIT_MS=1
    ;;
    read-sectors)
      TAG=disk
      FIELD=3
    ;;
    write-sectors)
      TAG=disk
      FIELD=7
    ;;
    *)
      shift
      continue
    ;;
  esac
  # The 'boot-complete' upstart job tests for whether an event has
  # been recorded by calling this script, so it's not an error if
  # the requested event doesn't exist.
  EVENTFILE="/tmp/${TAG}-${EVENT}"
  if [ -f "$EVENTFILE" ]; then
    result=$(getfield $FIELD $(tail -1 "$EVENTFILE"))
    if [ -n "$EMIT_MS" ]; then
      result=$(seconds_to_ms $result)
    fi
    echo $result
  fi
  shift
done
